<template>
    <div>
        <div style="margin-bottom: 20px;">
            <el-input v-model="query.name" placeholder="请输入名称" style="width: 200px;margin-right: 20px;"></el-input>
            <el-select v-model="query.type" placeholder="请选择类别" style="margin-right: 20px;" clearable>
                <el-option label="猫咪" value="1"></el-option>
                <el-option label="狗狗" value="2"></el-option>
            </el-select>
            <el-select v-model="query.sex" placeholder="请选择性别" style="margin-right: 20px;" clearable>
                <el-option label="公" value="1"></el-option>
                <el-option label="母" value="2"></el-option>
            </el-select>
            <el-button type="primary" @click="select">查询</el-button>
        </div>
        <div >
            <el-button type="primary" @click="add(null)">新增宠物</el-button>
            <el-button type="danger" @click="handDelete" :disabled="!ids.length">批量删除</el-button>

        </div>
        <el-table border :data="list" style="width: 100%;margin-top: 20px;"  @selection-change="handleSelectionChange">
            <el-table-column
            type="selection"
            width="55">
          </el-table-column>
            <el-table-column prop="name" label="名称" />
            <el-table-column prop="pinzhong" label="品种" />
            <el-table-column prop="tizhong" label="体重(kg)" />
            <el-table-column prop="age" label="年龄" show-overflow-tooltip/>
            <el-table-column prop="age" label="用户" >
                <template #default="scope">
                    {{getName(scope.row.userId)}}
                </template>
</el-table-column>
<el-table-column prop="price" label="类别">
    <template #default="scope">
                    {{scope.row.type==1?'猫咪':'狗狗'}}
                </template>
</el-table-column>
<el-table-column prop="price" label="性别">
    <template #default="scope">
        {{scope.row.sex==1?'公':'母'}}
    </template>
</el-table-column>
<el-table-column prop="img" label="图片">
    <template #default="scope">
                    <img :src="scope.row.img" alt="" v-if="scope.row.img" style="width: 100px;height: 100px;">
                    <span v-else>暂无图片</span>
                 </template>
</el-table-column>
<el-table-column label="操作">
    <template #default="scope">
                    <!-- {{scope.row}} -->
                    <el-button type="text" @click="add(scope.row)">修改</el-button>
                    <el-button type="text" @click="handDelete(scope.row)">删除</el-button>
                </template>
</el-table-column>
<!-- <el-table-column prop="address" label="Address" /> -->
</el-table>
<el-pagination v-show="total>0" style="margin-top: 20px;" :currentPage="query.page" :page-sizes="[100, 200, 300, 400]" background layout="total,  prev, pager, next, jumper" :page-size="query.size" :total="total" @current-change="currentChange"></el-pagination>
<el-dialog destroy-on-close v-model="dialogVisible" :title="type=='add'?'新增宠物':'修改宠物'" width="30%" :before-close="handleClose">
    <el-form :model="form" :rules="rules" ref="formRef" :inline="false" size="default">
        <el-form-item prop="name" style="width: 100%;">
            <el-input v-model="form.name" placeholder="请输入名称"></el-input>
        </el-form-item>
        <el-form-item prop="type" style="width: 100%;">
            <el-select v-model="form.type" placeholder="请选择类别" style="width: 100%;" clearable>
                <el-option label="猫咪" value="1"></el-option>
                <el-option label="狗狗" value="2"></el-option>
            </el-select>
        </el-form-item>

        <el-form-item prop="userId" style="width: 100%;" v-if="store.state.user.type==3">
            <el-select v-model="form.userId" placeholder="请选择用户" style="width: 100%;" clearable>
                <el-option :label="v.name" :value="v.id" v-for="v in userList"></el-option>
            </el-select>
        </el-form-item>
        <el-form-item prop="sex" style="width: 100%;">
            <el-select v-model="form.sex" placeholder="请选择性别" style="width: 100%;" clearable>
                <el-option label="公" value="1"></el-option>
                <el-option label="母" value="2"></el-option>
            </el-select>
        </el-form-item>

        <el-form-item prop="pinzhong" style="width: 100%;">
            <el-input v-model="form.pinzhong" placeholder="请输入品种"></el-input>
        </el-form-item>
        <el-form-item prop="age" style="width: 100%;">
            <el-input v-model="form.age" placeholder="请输入年龄" type="number"></el-input>
        </el-form-item>
        <el-form-item prop="tizhong" style="width: 100%;">
            <el-input v-model="form.tizhong" placeholder="请输入体重" type="number">
                <template #append>kg</template>
            </el-input>
        </el-form-item>
        <el-form-item>
            <el-upload class="avatar-uploader" action="/api/upload" :show-file-list="false" :on-success="handleAvatarSuccess" :before-upload="beforeAvatarUpload">
                <img v-if="form.img" :src="form.img" class="avatar" />
                <el-icon v-else class="avatar-uploader-icon">
                    <Plus />
                </el-icon>
            </el-upload>
        </el-form-item>
    </el-form>
    <template #footer>
  <span class="dialog-footer">
    <el-button @click="dialogVisible = false">取消</el-button>
    <el-button type="primary" @click="submit">
      确定
    </el-button>
  </span>
</template>
</el-dialog>
</div>
</template>
<script setup>
    import {
        onMounted,
        ref,
        reactive
    } from 'vue'
    import {
        chongwuList,
        getUserList,
        deptAdd,
        deptDelete,
        chongwudeletee,
        regchongwu,
        chongwuUpdate
    } from "~/api/xm.js"
    import {
        useStore
    } from 'vuex'
    import {
        ElMessage,
        ElMessageBox
    } from 'element-plus'
    import {
        Plus
    } from '@element-plus/icons-vue'
    const store = useStore()
    const ids = ref([])
    const list = ref([])
    const total = ref(0)
    const type = ref('')
    const id = ref('')
    const userList = ref([])
    const dialogVisible = ref(false)
    const query = ref({
        page: 1,
        size: 10,
        userId: store.state.user.type == 1 ? store.state.user.id : null
    })
    const form = reactive({
        name: "",
        avatar: ''
    })

    function handleAvatarSuccess(e) {
        form.img = e.data
        console.log(e);
    }

    function currentChange(e) {
        query.value.page = e
        select()
        console.log(e);
    }

    function getName(id) {
        let item = userList.value.find(v => v.id == id)
        return item && item.name
    }

    function handleSelectionChange(e) {

        ids.value = e.map(v => v.id)
        console.log(ids.value);
    }
    const rules = {
        type: [{
            required: true,
            message: '类型不能为空',
            trigger: 'change',
        }],
        userId: [{
            required: true,
            message: '用户不能为空',
            trigger: 'change',
        }],
        name: [{
            required: true,
            message: '名称不能为空',
            trigger: 'change',
        }],
        sex: [{
            required: true,
            message: '性别不能为空',
            trigger: 'change',
        }],
        age: [{
            required: true,
            message: '年龄不能为空',
            trigger: 'change',
        }],
        tizhong: [{
            required: true,
            message: '体重不能为空',
            trigger: 'change',
        }],
        pinzhong: [{
            required: true,
            message: '品种不能为空',
            trigger: 'change',
        }]
    }
    onMounted(() => {
        select()
        getUserList({
            page: 1,
            size: 999,
            type: 1
        }).then(res => {
            userList.value = res.data.list
            console.log(res.data.list);
        })
    })

    function handDelete(row) {
        let id = row.id ? row.id : ids.value.join(',')
        console.log(id);
        ElMessageBox.confirm(
                `是否删除id为${id}的数据?`,
                'Warning', {
                    confirmButtonText: '确定删除',
                    cancelButtonText: '取消删除',
                    type: 'warning',
                }
            )
            .then(() => {
                chongwudeletee(id).then(res => {
                    if (res.code == 200) {
                        ElMessage({
                            type: 'success',
                            message: 'Delete completed',
                        })
                        select()
                    }
                })

            })

    }

    function add(e) {
        form.userId = ''
        form.id = ''
        form.name = ''
        form.pinzhong = ''
        form.img = ''
        form.tizhong = ''
        form.age = ''
        form.type = ''
        form.sex = ''
        type.value = "add"
        dialogVisible.value = true
        if (e && e.id) {
            if (store.state.user.type == 3) {
                form.userId = e.userId
            }
            type.value = "edit"
            form.id = e.id
            form.name = e.name
            form.pinzhong = e.pinzhong
            form.tizhong = e.tizhong
            form.age = e.age
            form.type = e.type + ''
            form.sex = e.sex + ''
            form.img = e.img
        }
    }

    function select() {
        chongwuList(query.value).then(res => {
            total.value = res.data.total
            list.value = res.data.list
            console.log(res);
        })
    }
    const formRef = ref(null)
    const rowId = ref('')

    function submit() {
        formRef.value.validate((valid) => {
            if (valid) {
                if (!form.userId) {
                    form.userId = store.state.user.id
                }
                if (type.value == "add") {
                    regchongwu(form).then(res => {
                        if (res.code == 200) {
                            ElMessage({
                                type: 'success',
                                message: '新增成功',
                            })
                            dialogVisible.value = false
                            select()
                        }

                    })
                } else {
                    chongwuUpdate(form).then(res => {
                        if (res.code == 200) {
                            ElMessage({
                                type: 'success',
                                message: '修改成功',
                            })
                            dialogVisible.value = false
                            select()
                        }
                    })
                }


            }
        })
    }
</script>
<style scoped>
    .avatar-uploader .avatar {
        width: 178px;
        height: 178px;
        display: block;
    }
</style>

<style>
    .avatar-uploader .el-upload {
        border: 1px dashed var(--el-border-color);
        border-radius: 6px;
        cursor: pointer;
        position: relative;
        overflow: hidden;
        transition: var(--el-transition-duration-fast);
    }
    
    .avatar-uploader .el-upload:hover {
        border-color: var(--el-color-primary);
    }
    
    .el-icon.avatar-uploader-icon {
        font-size: 28px;
        color: #8c939d;
        width: 178px;
        height: 178px;
        text-align: center;
    }
</style>